package org.tis.tools.abf.module.om.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.ac.entity.AcOperator;
import org.tis.tools.abf.module.ac.entity.vo.OperatorEmp;
import org.tis.tools.abf.module.ac.service.IAcOperatorService;
import org.tis.tools.abf.module.common.entity.enums.YON;
import org.tis.tools.abf.module.om.controller.request.OmEmployeeAddRequest;
import org.tis.tools.abf.module.om.controller.request.OmEmployeeByOrgAndPositionRequest;
import org.tis.tools.abf.module.om.controller.request.OmEmployeeUpdateRequest;
import org.tis.tools.abf.module.om.dao.OmEmployeeMapper;
import org.tis.tools.abf.module.om.entity.*;
import org.tis.tools.abf.module.om.entity.enums.OmEmployeeStatus;
import org.tis.tools.abf.module.om.entity.enums.OmPositionType;
import org.tis.tools.abf.module.om.entity.vo.OmEmployeeExit;
import org.tis.tools.abf.module.om.entity.vo.OmEmployeeForPositionDetail;
import org.tis.tools.abf.module.om.entity.vo.OmEmployeeOperatorDetail;
import org.tis.tools.abf.module.om.exception.OMExceptionCodes;
import org.tis.tools.abf.module.om.exception.OrgManagementException;
import org.tis.tools.abf.module.om.service.*;
import org.tis.tools.core.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

import static org.tis.tools.core.utils.BasicUtil.wrap;


/**
 * omEmployee的Service接口实现类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OmEmployeeServiceImpl extends ServiceImpl<OmEmployeeMapper, OmEmployee> implements IOmEmployeeService {

    @Autowired
    private IOmOrgService omOrgService;
    @Autowired
    private IOmPositionService omPositionService;
    @Autowired
    private IAcOperatorService operatorService;
    @Autowired
    private IOmEmpOrgService empOrgService;
    @Autowired
    private IOmEmpOrgService omEmpOrgService;
    @Autowired
    private IOmEmpPositionService omEmpPositionService;
    @Autowired
    private IOmEmpGroupService omEmpGroupService;


    @Override
    public List<OmEmployee> queryEmployeeByGuid(String orgGuid) {

        EntityWrapper<OmEmpOrg> omEmpOrgEntityWrapper = new EntityWrapper<>();
        omEmpOrgEntityWrapper.eq(OmEmpOrg.COLUMN_GUID_ORG, orgGuid);
        List<OmEmpOrg> list = empOrgService.selectList(omEmpOrgEntityWrapper);
        if(list.isEmpty()){
            return null;
        }else{
            List<String> list2 = new ArrayList<>();
            for (OmEmpOrg oeo : list) {
                list2.add(oeo.getGuidEmp());
            }
            EntityWrapper<OmEmployee> omEmployeeEntityWrapper = new EntityWrapper<>();
            omEmployeeEntityWrapper.in(OmEmployee.COLUMN_GUID, list2);
            List<OmEmployee> omEmployeeList = this.baseMapper.selectList(omEmployeeEntityWrapper);
            return omEmployeeList;
        }
    }

    @Override
    public void add(OmEmployeeAddRequest omEmployeeAddRequest) throws OrgManagementException {

        //判断机构是否存在
        OmOrg omOrg = omOrgService.selectById(omEmployeeAddRequest.getGuidOrg());
        if (omOrg == null){
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_OM_ORG,wrap("组织GUID对应的组织不存在",omEmployeeAddRequest.getGuidOrg()));
        }
        //判断岗位是否存在
        if (StringUtil.isNotEmpty(omEmployeeAddRequest.getGuidPosition())){
            OmPosition omPosition = omPositionService.selectById(omEmployeeAddRequest.getGuidPosition());
            if (omPosition == null){
                throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_OM_POSITION,wrap("岗位GUID对应的岗位不存在",omEmployeeAddRequest.getGuidPosition()));
            }
        }

        OmEmployee omEmployee = new OmEmployee();

        //新建人员后停留在在招状态
        omEmployee.setEmpstatus(OmEmployeeStatus.OFFER);
        //收集参数
        omEmployee.setEmpCode(omEmployeeAddRequest.getEmpCode());
        omEmployee.setEmpName(omEmployeeAddRequest.getEmpName());
        omEmployee.setEmpRealname(omEmployeeAddRequest.getEmpRealname());
        omEmployee.setGender(omEmployeeAddRequest.getGender());
        omEmployee.setGuidOrg(omEmployeeAddRequest.getGuidOrg());
        omEmployee.setGuidOperator(omEmployeeAddRequest.getGuidOperator());
        omEmployee.setGuidPosition(omEmployeeAddRequest.getGuidPosition() == null ? "":omEmployeeAddRequest.getGuidPosition());
        omEmployee.setGuidEmpMajor(omEmployeeAddRequest.getGuidEmpMajor());
        omEmployee.setIndate(omEmployeeAddRequest.getIndate());
        omEmployee.setOutdate(omEmployeeAddRequest.getOutdate());
        omEmployee.setMobileno(omEmployeeAddRequest.getMobileno());
        omEmployee.setPaperType(omEmployeeAddRequest.getPaperType());
        omEmployee.setPaperNo(omEmployeeAddRequest.getPaperNo());
        omEmployee.setBirthdate(omEmployeeAddRequest.getBirthdate());
        omEmployee.setHtel(omEmployeeAddRequest.getHtel());
        omEmployee.setHaddress(omEmployeeAddRequest.getHaddress());
        omEmployee.setHzipcode(omEmployeeAddRequest.getHzipcode());
        omEmployee.setUserId(omEmployeeAddRequest.getUserId());
        omEmployee.setRemark(omEmployeeAddRequest.getRemark());

        insert(omEmployee);

        //为员工隶属机构新增一条信息
        OmEmpOrg omEmpOrg = new OmEmpOrg();
        omEmpOrg.setGuidOrg(omEmployee.getGuidOrg());
        omEmpOrg.setGuidEmp(omEmployee.getGuid());
        omEmpOrg.setIsmain(YON.YES);
        omEmpOrgService.insert(omEmpOrg);

        if (StringUtil.isNotEmpty(omEmployeeAddRequest.getGuidPosition())){
            OmEmpPosition omEmpPosition = new OmEmpPosition();
            omEmpPosition.setGuidPosition(omEmployee.getGuidPosition());
            omEmpPosition.setGuidEmp(omEmployee.getGuid());
            omEmpPosition.setIsmain(YON.YES);
            omEmpPositionService.insert(omEmpPosition);
        }
    }

    @Override
    public OmEmployee change(OmEmployeeUpdateRequest omEmployeeUpdateRequest) throws OrgManagementException {

        //判断机构是否存在
        OmOrg omOrg = omOrgService.selectById(omEmployeeUpdateRequest.getGuidOrg());
        if (omOrg == null) {
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_OM_ORG, wrap("组织GUID对应的组织不存在", omEmployeeUpdateRequest
                    .getGuidOrg()));
        }
        //判断岗位是否存在
        if (StringUtil.isNotEmpty(omEmployeeUpdateRequest.getGuidPosition())) {
            OmPosition omPosition = omPositionService.selectById(omEmployeeUpdateRequest.getGuidPosition());
            if (omPosition == null) {
                throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_OM_POSITION, wrap("岗位GUID对应的岗位不存在", omEmployeeUpdateRequest.getGuidPosition()));
            }
        }
        //如果操作员ID存在,判断操作员是否存在
        if (!"".equals(omEmployeeUpdateRequest.getGuidOperator())) {
            AcOperator acOperator = operatorService.selectById(omEmployeeUpdateRequest.getGuidOperator());
            if (acOperator == null) {
                throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_AC_OPERATOR, wrap("应用GUID对应的应用不存在", omEmployeeUpdateRequest.getGuidOperator()));
            }
        }

        OmEmployee omEmployeeQue = selectById(omEmployeeUpdateRequest.getGuid());

        OmEmployee omEmployee = new OmEmployee();
        //收集参数
        omEmployee.setGuid(omEmployeeUpdateRequest.getGuid());
        omEmployee.setEmpCode(omEmployeeUpdateRequest.getEmpCode());
        omEmployee.setEmpName(omEmployeeUpdateRequest.getEmpName());
        omEmployee.setEmpRealname(omEmployeeUpdateRequest.getEmpRealname());
        omEmployee.setGender(omEmployeeUpdateRequest.getGender());
        omEmployee.setEmpstatus(omEmployeeUpdateRequest.getEmpstatus());
        omEmployee.setGuidOrg(omEmployeeUpdateRequest.getGuidOrg());
        omEmployee.setGuidOperator(omEmployeeUpdateRequest.getGuidOperator());
        omEmployee.setGuidPosition(omEmployeeUpdateRequest.getGuidPosition());
        omEmployee.setGuidEmpMajor(omEmployeeUpdateRequest.getGuidEmpMajor());
        omEmployee.setIndate(omEmployeeUpdateRequest.getIndate());
        omEmployee.setOutdate(omEmployeeUpdateRequest.getOutdate());
        omEmployee.setMobileno(omEmployeeUpdateRequest.getMobileno());
        omEmployee.setPaperType(omEmployeeUpdateRequest.getPaperType());
        omEmployee.setPaperNo(omEmployeeUpdateRequest.getPaperNo());
        omEmployee.setBirthdate(omEmployeeUpdateRequest.getBirthdate());
        omEmployee.setHtel(omEmployeeUpdateRequest.getHtel());
        omEmployee.setHaddress(omEmployeeUpdateRequest.getHaddress());
        omEmployee.setHzipcode(omEmployeeUpdateRequest.getHzipcode());
        omEmployee.setUserId(omEmployeeUpdateRequest.getUserId());
        omEmployee.setRemark(omEmployeeUpdateRequest.getRemark());

        updateById(omEmployee);

        //判断员工是否修改了岗位,如果修改则同步修改员工和岗位的关系
        if (StringUtil.isNotEmpty(omEmployeeQue.getGuidPosition())) {
            if (!omEmployeeQue.getGuidPosition().equals(omEmployee.getGuidPosition())) {
                //查询之前员工和岗位数据
                if (StringUtil.isNotEmpty(omEmployeeQue.getGuidPosition())) {

                    Wrapper<OmEmpPosition> wrapper = new EntityWrapper<>();
                    wrapper.eq(OmEmpPosition.COLUMN_GUID_POSITION, omEmployeeQue.getGuidPosition());
                    wrapper.eq(OmEmpPosition.COLUMN_GUID_EMP, omEmployeeQue.getGuid());

                    OmEmpPosition omEmpPosition = omEmpPositionService.selectOne(wrapper);
                    if (null != omEmpPosition) {
                        omEmpPositionService.deleteById(omEmpPosition.getGuid());
                    }
                }

                //添加一条新的记录
                if (StringUtil.isNotEmpty(omEmployee.getGuidPosition())) {

                    OmEmpPosition omEmpPositionAdd = new OmEmpPosition();
                    omEmpPositionAdd.setGuidEmp(omEmployee.getGuid());
                    omEmpPositionAdd.setGuidPosition(omEmployee.getGuidPosition());
                    omEmpPositionAdd.setIsmain(YON.YES);
                    omEmpPositionService.insert(omEmpPositionAdd);
                }
            }
        }
        return omEmployee;
    }

    /**
     * 删除员工
     */
    @Override
    public void moveEmp(String id) throws OrgManagementException {

        boolean isDelete = deleteById(id);
        if (isDelete){
            //删除员工和机构之间的关系
            Wrapper<OmEmpOrg> wrapperOrg = new EntityWrapper<>();
            wrapperOrg.eq(OmEmpOrg.COLUMN_GUID_EMP,id);
            List<OmEmpOrg> listOrg = omEmpOrgService.selectList(wrapperOrg);
            if (0 != listOrg.size()){
                //进行删除
                boolean deleteEmpOrg = omEmpOrgService.delete(wrapperOrg);
                if (!deleteEmpOrg){
                    throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_DELETE_OM_EMP_ORG,wrap
                            ("删除员工和机构之间的关系失败"));
                }
            }
            //删除员工和岗位之间的关系
            Wrapper<OmEmpPosition> wrapperPosition = new EntityWrapper<>();
            wrapperPosition.eq(OmEmpPosition.COLUMN_GUID_EMP,id);
            List<OmEmpPosition> listPosition = omEmpPositionService.selectList(wrapperPosition);
            if (0 != listPosition.size()){
                //进行删除
                boolean deleteEmpPosition = omEmpPositionService.delete(wrapperPosition);
                if (!deleteEmpPosition){
                    throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_DELETE_OM_EMP_POSITION,wrap
                            ("删除员工和岗位之间的关系失败"));
                }
            }
            //删除员工和工作组之间的关系
            Wrapper<OmEmpGroup> wrapperGroup = new EntityWrapper<>();
            wrapperGroup.eq(OmEmpPosition.COLUMN_GUID_EMP,id);
            List<OmEmpGroup> listGroup = omEmpGroupService.selectList(wrapperGroup);
            if (0 != listGroup.size()){
                //进行删除
                boolean deleteEmpGroup = omEmpGroupService.delete(wrapperGroup);
                if (!deleteEmpGroup){
                    throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_DELETE_OM_EMP_GROUP,wrap
                            ("删除员工和工作组之间的关系失败"));
                }
            }
        }
    }

    /**
     *根据机构查询员工
     */
    @Override
    public Page<OmEmployeeForPositionDetail> queryEmpByOrg(Page<OmEmployee> page, Wrapper<OmEmployee> wrapper, String id) throws OrgManagementException {

        //2018年8月30日 09:24:21 自定义模糊查询开始
        Page<OmEmployee> pageOrg = new Page<>();

        if (null == wrapper.getEntity()){
            //没有任何参数
            pageOrg = page.setRecords(this.baseMapper.queryEmpByOrg(page,id));
        }else {
            //有参数
            OmEmployee omEmployee = wrapper.getEntity();
            String empCode = null;
            String empName = null;
            String guidPosition = null;
            String guidEmpMajor = null;

            if (!StringUtil.isEmpty(omEmployee.getEmpCode())){
                empCode = "%"+omEmployee.getEmpCode()+"%";
            }
            if (!StringUtil.isEmpty(omEmployee.getEmpName())){
                empName = "%"+omEmployee.getEmpName()+"%";
            }
            if (!StringUtil.isEmpty(omEmployee.getGuidPosition())){
                guidPosition = omEmployee.getGuidPosition();
            }
            if (!StringUtil.isEmpty(omEmployee.getGuidEmpMajor())){
                guidEmpMajor = omEmployee.getGuidEmpMajor();
            }

            pageOrg = page.setRecords(this.baseMapper.queryEmpByOrgFilter(page,id,empCode,empName,omEmployee.getGender(),
                    guidPosition,guidEmpMajor,omEmployee.getEmpstatus()));
        }
        List<OmEmployee> listEmp = page.getRecords();
        //2018年8月30日 09:24:21 自定义模糊查询结束

        Page<OmEmployeeForPositionDetail> pageForPosition = new Page<>();
        List<OmEmployeeForPositionDetail> listPosition = new ArrayList<>();

        //查询机构下岗位GUID
        Wrapper<OmPosition> positionWrapper = new EntityWrapper<>();
        positionWrapper.eq(OmPosition.COLUMN_GUID_MAPPING,id);
        positionWrapper.eq(OmPosition.COLUMN_POSITION_TYPE, OmPositionType.ORGANIZATION);
        List<OmPosition> positionList = omPositionService.selectList(positionWrapper);

        for (OmEmployee omEmployee : listEmp){
            String omPositionName = "";
            if (0 != positionList.size()){
                for (OmPosition omPosition : positionList){
                    if (null != omPosition){
                        Wrapper<OmEmpPosition> empPositionWrapper = new EntityWrapper<>();
                        empPositionWrapper.eq(OmEmpPosition.COLUMN_GUID_EMP,omEmployee.getGuid());
                        empPositionWrapper.eq(OmEmpPosition.COLUMN_GUID_POSITION,omPosition.getGuid());
                        OmEmpPosition omEmpPosition = omEmpPositionService.selectOne(empPositionWrapper);
                        if (null != omEmpPosition){
                            omPositionName = omPosition.getPositionName();
                        }
                    }
                }
            }
            OmEmployeeForPositionDetail omEmployeeForPositionDetail = new OmEmployeeForPositionDetail(omEmployee,omPositionName);

            //将查询回来的信息放回到list中
            listPosition.add(omEmployeeForPositionDetail);
        }

        pageForPosition.setCondition(pageOrg.getCondition());
        pageForPosition.setRecords(listPosition);
        pageForPosition.setTotal(pageOrg.getTotal());
        pageForPosition.setSize(pageOrg.getSize());
        pageForPosition.setCurrent(pageOrg.getCurrent());

        return pageForPosition;
    }


    @Override
    public List<OmEmployee> queryEmpListByOrg(String id) throws OrgManagementException {

        Wrapper<OmEmpOrg> wrapper = new EntityWrapper<>();
        wrapper.eq(OmEmpOrg.COLUMN_GUID_ORG,id);
        //查询出机构和员工关系表中的所有信息
        List<OmEmpOrg> listorg = omEmpOrgService.selectList(wrapper);

        //查询出所有相关的员工
        List<OmEmployee> listEmp = new ArrayList<>();
        for (OmEmpOrg omEmpOrg:listorg){
            if (null != omEmpOrg){
                OmEmployee omEmployee = selectById(omEmpOrg.getGuidEmp());
                if (null != omEmployee){
                    listEmp.add(omEmployee);
                }
            }
        }

        return listEmp;
    }


    @Override
    public OmEmployee onJob(OmEmployee omEmployee) throws OrgManagementException {

        OmEmployee omEmployeeQue = selectById(omEmployee.getGuid());

        String guidOperator = omEmployee.getGuidOperator();
        String userId = omEmployee.getUserId();

        if (!(null == guidOperator || "".equals(guidOperator))){
            userId = queryUserIdByOpertary(guidOperator);
        }
        if (!(null == userId || "".equals(userId))){
            guidOperator = queryGuidByUserId(userId);
        }
        if (null == omEmployee.getIndate()){
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_OM_EMPLOYEE_ONJOB,wrap
                    ("员工入职日期不能为空"));
        }

        omEmployeeQue.setGuidOperator(guidOperator);
        omEmployeeQue.setUserId(userId);
        omEmployeeQue.setIndate(omEmployee.getIndate());
        omEmployeeQue.setEmpstatus(OmEmployeeStatus.ONJOB);

        updateById(omEmployeeQue);
        return omEmployeeQue;
    }


    @Override
    public OmEmployee changeOnJob(OmEmployee omEmployee) throws OrgManagementException {

        OmEmployee omEmployeeQue = selectById(omEmployee.getGuid());

        String guidOperator = omEmployee.getGuidOperator();
        String userId = omEmployee.getUserId();

        if (!(null == guidOperator || "".equals(guidOperator))){
            userId = queryUserIdByOpertary(guidOperator);
        }
        if (!(null == userId || "".equals(userId))){
            guidOperator = queryGuidByUserId(userId);
        }
        if (null == omEmployee.getIndate()){
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_UPDATE_OM_EMPLOYEE_ONJOB,wrap
                    ("修改员工入职信息时入职时间不能为空"));
        }

        omEmployeeQue.setGuidOperator(guidOperator);
        omEmployeeQue.setUserId(userId);
        omEmployeeQue.setIndate(omEmployee.getIndate());

        updateAllColumnById(omEmployeeQue);
        return omEmployeeQue;
    }

    @Override
    public OmEmployee outJob(OmEmployee omEmployee) throws OrgManagementException {

        OmEmployee omEmployeeQue = selectById(omEmployee.getGuid());

        if (null == omEmployee.getOutdate()){
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_OM_EMPLOYEE_OUTJOB,wrap
                    ("员工离职日期不能为空"));
        }

        omEmployeeQue.setGuidOperator(omEmployee.getGuidOperator());
        omEmployeeQue.setUserId(omEmployee.getUserId());
        omEmployeeQue.setOutdate(omEmployee.getOutdate());
        omEmployeeQue.setEmpstatus(OmEmployeeStatus.OFFJOB);

        updateById(omEmployeeQue);
        return omEmployeeQue;
    }

    private String queryUserIdByOpertary(String guidOperator){

        String userId = null;

        AcOperator acOperator = operatorService.selectById(guidOperator);
        if (null != acOperator){
            userId = acOperator.getUserId();
        }else {
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_AC_OPERATOR,wrap("查询操作员失败",guidOperator));
        }

        return userId;
    }

    private String queryGuidByUserId(String userId){

        String guidOperator = null;

        Wrapper<AcOperator> wrapper = new EntityWrapper<>();
        wrapper.eq(AcOperator.COLUMN_USER_ID,userId);
        AcOperator acOperator = operatorService.selectOne(wrapper);
        if (null != acOperator){
            guidOperator = acOperator.getGuid();
        }else {
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_QUERY_AC_OPERATOR,wrap("查询操作员失败",userId));
        }

        return guidOperator;
    }

    @Override
    public Page<OmEmployeeOperatorDetail> queryByOrgPosition(Page<OmEmployee> page, String orgId, String positionId) throws OrgManagementException {

        Page<OmEmployeeOperatorDetail> employeeOperatorDetailPage = new Page<>();
        List<OmEmployeeOperatorDetail> employeeOperatorDetailList= new ArrayList<>();

        Page<OmEmployee> employeePage = page.setRecords(this.baseMapper.queryByOrgPosition(page,positionId));
        List<OmEmployee> employeeList = employeePage.getRecords();

        for (OmEmployee omEmployee : employeeList){
            if (null != omEmployee){
                if (!StringUtil.isEmpty(omEmployee.getGuidOperator())){
                    AcOperator acOperator = operatorService.selectById(omEmployee.getGuidOperator());
                    if (null != acOperator){
                        OmEmployeeOperatorDetail omEmployeeOperatorDetail = new OmEmployeeOperatorDetail(omEmployee,
                                acOperator.getOperatorName());
                        employeeOperatorDetailList.add(omEmployeeOperatorDetail);
                    }else {
                        OmEmployeeOperatorDetail omEmployeeOperatorDetail = new OmEmployeeOperatorDetail(omEmployee,
                                "");
                        employeeOperatorDetailList.add(omEmployeeOperatorDetail);
                    }
                }else {
                    OmEmployeeOperatorDetail omEmployeeOperatorDetail = new OmEmployeeOperatorDetail(omEmployee,
                            "");
                    employeeOperatorDetailList.add(omEmployeeOperatorDetail);
                }
            }
        }

        employeeOperatorDetailPage.setCondition(employeePage.getCondition());
        employeeOperatorDetailPage.setRecords(employeeOperatorDetailList);
        employeeOperatorDetailPage.setTotal(employeePage.getTotal());
        employeeOperatorDetailPage.setSize(employeePage.getSize());
        employeeOperatorDetailPage.setCurrent(employeePage.getCurrent());

        return employeeOperatorDetailPage;
    }


    @Override
    public List<OmEmployee> getOtherEmp(OmEmployeeByOrgAndPositionRequest om) throws OrgManagementException {

        //查询机构下的所有员工
        Wrapper<OmEmpOrg> wrapperOrg = new EntityWrapper<>();
        wrapperOrg.eq(OmEmpOrg.COLUMN_GUID_ORG,om.getGuidOrg());
        List<OmEmpOrg> listOrg = omEmpOrgService.selectList(wrapperOrg);

        //查询机构下的所有员工
        List<OmEmployee> listByOrg = new ArrayList<>();
        for (OmEmpOrg omEmpOrg:listOrg){
            if (null != omEmpOrg){
                OmEmployee omEmployee = selectById(omEmpOrg.getGuidEmp());
                if (null != omEmployee){
                    listByOrg.add(omEmployee);
                }
            }
        }

        //查询岗位下的所有员工
        Wrapper<OmEmpPosition> wrapperPosition = new EntityWrapper<>();
        wrapperPosition.eq(OmEmpPosition.COLUMN_GUID_POSITION,om.getGuidPosition());
        List<OmEmpPosition> listPosition = omEmpPositionService.selectList(wrapperPosition);

        //查询岗位下的所有员工
        List<OmEmployee> listByPosition = new ArrayList<>();
        for (OmEmpPosition omEmpPosition:listPosition){
            if (null != omEmpPosition){
                OmEmployee omEmployee = selectById(omEmpPosition.getGuidEmp());
                if (null != omEmployee){
                    listByPosition.add(omEmployee);
                }
            }
        }

        listByOrg.removeAll(listByPosition);
        return listByOrg;
    }


    @Override
    public void addInOrgAndPosition(OmEmployee omEmployee) throws OrgManagementException {

        boolean empOrgExist = false;
        boolean empPositionExist = false;

        if (!"".equals(omEmployee.getGuidOrg())){
            Wrapper<OmEmpOrg> wrapper = new EntityWrapper<>();
            wrapper.eq(OmEmpOrg.COLUMN_GUID_EMP,omEmployee.getGuid());
            wrapper.eq(OmEmpOrg.COLUMN_GUID_ORG,omEmployee.getGuidOrg());
            OmEmpOrg omEmpOrg = omEmpOrgService.selectOne(wrapper);
            if (null == omEmpOrg){
                OmEmpOrg omEmpOrgAdd = new OmEmpOrg();
                omEmpOrgAdd.setGuidEmp(omEmployee.getGuid());
                omEmpOrgAdd.setGuidOrg(omEmployee.getGuidOrg());
                omEmpOrgAdd.setIsmain(YON.NO);
                omEmpOrgService.insert(omEmpOrgAdd);
            }else {
               empOrgExist = true;
            }
        }

        if (!"".equals(omEmployee.getGuidPosition())){
            Wrapper<OmEmpPosition> wrapper = new EntityWrapper<>();
            wrapper.eq(OmEmpPosition.COLUMN_GUID_EMP,omEmployee.getGuid());
            wrapper.eq(OmEmpPosition.COLUMN_GUID_POSITION,omEmployee.getGuidPosition());
            OmEmpPosition omEmpPosition = omEmpPositionService.selectOne(wrapper);
            if (null == omEmpPosition){
                OmEmpPosition omEmpPositionAdd = new OmEmpPosition();
                omEmpPositionAdd.setGuidEmp(omEmployee.getGuid());
                omEmpPositionAdd.setGuidPosition(omEmployee.getGuidPosition());
                omEmpPositionAdd.setIsmain(YON.NO);
                omEmpPositionService.insert(omEmpPositionAdd);
            }else {
                empPositionExist = true;
            }
        }

        if (empOrgExist && empPositionExist){
            throw new OrgManagementException(OMExceptionCodes.FAILURE_WHEN_CREATE_OM_EMP_ORG_AND_POSITION,wrap("该员工已存在于该机构的岗位下"));
        }
    }


    @Override
    public Page<OperatorEmp> queryEmpByName(Page<OmEmployee> page, Wrapper<OmEmployee> wrapper ) throws OrgManagementException {

        //待返回的信息
        Page<OperatorEmp> operatorEmpPage = new Page<>();
        List<OperatorEmp> operatorEmpList = new ArrayList<>();
        //查询员工
        Page<OmEmployee> empPage = new Page<>();
        List<OmEmployee> empList = new ArrayList<>();

        if(null == wrapper){//"queryAll".equals(name)){
            //分页查询所有员工
            wrapper = new EntityWrapper<>();
            empPage = selectPage(page,wrapper);
            empList = empPage.getRecords();
        }else {
            //根据姓名查询员工
            wrapper.isNotNull(OmEmployee.COLUMN_USER_ID);
            empPage = selectPage(page,wrapper);
            empList = empPage.getRecords();
        }

        for (OmEmployee omEmployee : empList){
            if (null != omEmployee){

                //新建OperatorEmp对象
                OperatorEmp operatorEmp = new OperatorEmp();

                operatorEmp.setEmpCode(omEmployee.getEmpCode());
                operatorEmp.setEmpName(omEmployee.getEmpName());

                //根据员工信息查询员工所在主机构信息
                Wrapper<OmEmpOrg> empOrgWrapper = new EntityWrapper<>();
                empOrgWrapper.eq(OmEmpOrg.COLUMN_GUID_EMP,omEmployee.getGuid());
                empOrgWrapper.eq(OmEmpOrg.COLUMN_ISMAIN,YON.YES);

                OmEmpOrg omEmpOrg = omEmpOrgService.selectOne(empOrgWrapper);
                //如果非空则根据该机构id查询机构名称,否则根据员工的主机构id查询机构信息
                if (null != omEmpOrg){
                    OmOrg omOrg = omOrgService.selectById(omEmpOrg.getGuidOrg());
                    if (null != omOrg){
                        operatorEmp.setOrgName(omOrg.getOrgName());
                    }else {
                        operatorEmp.setOrgName("");
                    }
                }else {
                    OmOrg omOrg = omOrgService.selectById(omEmployee.getGuidOrg());
                    if (null != omOrg){
                        operatorEmp.setOrgName(omOrg.getOrgName());
                    }else {
                        operatorEmp.setOrgName("");
                    }
                }

                //根据员工的user_id查询对应的操作员信息
                if (StringUtil.isEmpty(omEmployee.getUserId())){
                    continue;
                }else {
                    Wrapper<AcOperator> operatorWrapper = new EntityWrapper<>();
                    operatorWrapper.eq(AcOperator.COLUMN_USER_ID, omEmployee.getUserId());

                    AcOperator acOperator = operatorService.selectOne(operatorWrapper);
                    if (null != acOperator) {
                        operatorEmp.setOperatorId(acOperator.getGuid());
                    } else {
                        operatorEmp.setOperatorId("");
                    }
                }
                operatorEmpList.add(operatorEmp);
            }
        }

        operatorEmpPage.setCondition(empPage.getCondition());
        operatorEmpPage.setRecords(operatorEmpList);
        operatorEmpPage.setTotal(empPage.getTotal());
        operatorEmpPage.setSize(empPage.getSize());
        operatorEmpPage.setCurrent(empPage.getCurrent());

        return operatorEmpPage;
    }

    /**
     * 根据机构查询已入职的员工
     */
    @Override
    public List<OmEmployee> queryEmp(String orgId) throws OrgManagementException {

        List<OmEmployee> list = this.baseMapper.queryByOrg(orgId);
        return list;
    }

    /**
     * 查询所有已入职的员工
     */
    @Override
    public List<OmEmployeeExit> queryEntryEmp(String roleId) throws OrgManagementException {

        //查询所有已入职员工
        List<OmEmployee> allEmployeeList = this.baseMapper.queryEntryEmp();
        //查询已分配的入职员工
        List<OmEmployee> entryEmployeeList = this.baseMapper.queryEntryEmpByRoleId(roleId);
        //获取未分配的入职员工
        allEmployeeList.removeAll(entryEmployeeList);

        List<OmEmployeeExit> exitList = new ArrayList<>();
        //为未分配的员工设置为"true"
        for (OmEmployee omEmployee : allEmployeeList){
            if (null != omEmployee){
                OmEmployeeExit omEmployeeExit = new OmEmployeeExit(omEmployee,"true");
                exitList.add(omEmployeeExit);
            }
        }
        //为已分配的员工设置为"false"
        for (OmEmployee omEmployee : entryEmployeeList){
            if (null != omEmployee){
                OmEmployeeExit omEmployeeExit = new OmEmployeeExit(omEmployee,"false");
                exitList.add(omEmployeeExit);
            }
        }

        return exitList;
    }

    /**
     * 根据机构查询角色下已分配和未分配的员工
     */
    @Override
    public List<OmEmployeeExit> queryEmployeesByOrgId(String orgId, String roleId) throws OrgManagementException {

        //机构下所有的入职员工
        List<OmEmployee> allEmployeeList = this.baseMapper.queryByOrg(orgId);
        //机构下的已分配的入职员工
        List<OmEmployee> entryEmployeeList = this.baseMapper.queryByOrgIdAndRoleId(orgId, roleId);
        //获取未分配的员工
        allEmployeeList.removeAll(entryEmployeeList);

        List<OmEmployeeExit> exitList = new ArrayList<>();
        //为未分配的员工设置为"true"
        for (OmEmployee omEmployee : allEmployeeList){
            if (null != omEmployee){
                OmEmployeeExit omEmployeeExit = new OmEmployeeExit(omEmployee,"true");
                exitList.add(omEmployeeExit);
            }
        }
        //为已分配的员工设置为"false"
        for (OmEmployee omEmployee : entryEmployeeList){
            if (null != omEmployee){
                OmEmployeeExit omEmployeeExit = new OmEmployeeExit(omEmployee,"false");
                exitList.add(omEmployeeExit);
            }
        }

        return exitList;
    }

    /**
     * 检查员工代码是否重复
     */
    @Override
    public Boolean queryEmpCode(String empCode) throws OrgManagementException {

        Boolean existEmpCode = false;

        Wrapper<OmEmployee> employeeWrapper = new EntityWrapper<>();
        employeeWrapper.eq(OmEmployee.COLUMN_EMP_CODE,empCode);

        OmEmployee omEmployee = selectOne(employeeWrapper);
        if (null != omEmployee){
            existEmpCode = true;
        }

        return existEmpCode;
    }


    /**
     * 检查修改时员工代码是否重复
     */
    @Override
    public Boolean queryEmpCodeForUpdate(String empCode, String guid) throws OrgManagementException {

        Boolean existEmpCode = false;

        Wrapper<OmEmployee> employeeWrapper = new EntityWrapper<>();
        employeeWrapper.eq(OmEmployee.COLUMN_EMP_CODE,empCode);
        employeeWrapper.notIn(OmEmployee.COLUMN_GUID,guid);

        OmEmployee omEmployee = selectOne(employeeWrapper);
        if (null != omEmployee){
            existEmpCode = true;
        }

        return existEmpCode;
    }

    /**
     * 查询可分配给岗位的员工
     */
    @Override
    public List<OmEmployee> queryAssignableEmployee(String guidOrg) throws OrgManagementException {

        //查询机构下员工
        List<OmEmployee> orgEmpList = this.baseMapper.queryByOrg(guidOrg);
        //查询机构下已分配给岗位的员工
        List<OmEmployee> allPositionEmpList = this.baseMapper.queryEmpInPosition(guidOrg);

        if (0 == orgEmpList.size()){
            return orgEmpList;
        }

        orgEmpList.removeAll(allPositionEmpList);
        return orgEmpList;
    }

    /**
     * 查询员工的可分配直接主管
     */
    @Override
    public List<OmEmployee> getEmployeeMajors(String guidOrg,String guid) throws OrgManagementException {

        List<String> guidEmpList = this.baseMapper.getEmpGuid(guidOrg);

        Wrapper<OmEmployee> employeeWrapper = new EntityWrapper<>();
        if (0 != guidEmpList.size()){
            employeeWrapper.in(OmEmployee.COLUMN_GUID,guidEmpList);
            employeeWrapper.eq(OmEmployee.COLUMN_EMPSTATUS,OmEmployeeStatus.ONJOB);
        }else {
            return null;
        }

        List<OmEmployee> employeeList = selectList(employeeWrapper);
        if (0 != employeeList.size()){
            if (StringUtil.isNotEmpty(guid)){
                OmEmployee omEmployee = selectById(guid);
                if (null != omEmployee){
                    employeeList.remove(omEmployee);
                }
            }
        }

        return employeeList;
    }
}

